import os
from moviepy.editor import VideoFileClip, concatenate_videoclips


def merge_videos_in_folder(folder_path, output_path):
    # 获取文件夹中所有的视频文件
    video_files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if
                   f.endswith(('.mp4', '.avi', '.mov', '.mkv'))]

    # 按文件名排序
    video_files.sort()

    # 读取所有视频文件
    video_clips = [VideoFileClip(video) for video in video_files]

    # 合并视频文件
    final_clip = concatenate_videoclips(video_clips)

    # 写入最终合并的视频文件
    final_clip.write_videofile(output_path)


# 使用示例
folder_path = 'path_to_your_folder'  # 替换为你的文件夹路径
output_path = 'output_video.mp4'  # 替换为你想要的输出视频文件名

merge_videos_in_folder(folder_path, output_path)
